import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/token.js'
// import Login from '@/views/Login'
// import User from '@/views/User'
// import Layout from '@/views/Layout'
// import Home from '@/views/Home'
// import Search from '@/views/Search'
// import SearchResult from '@/views/Search/SearchResult.vue'
// import ArticleDetail from '@/views/ArticleDetail/index.vue'
// import UserEdit from '@/views/User/UserEdit.vue'
// import Chat from '@/views/Chat'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/layout/home'
  },
  {
    path: '/detail',
    component: () => import(/* webpackChunkName:"Detail" */'@/views/ArticleDetail/index.vue')
  },
  {
    path: '/login',
    component: () => import(/* webpackChunkName:"Login" */ '@/views/Login'),
    beforeEach (to, from, next) {
      if (getToken()?.length > 0) {
        // next(false)
        next('/')
      } else {
        // 其他放行
        next()
      }
    }
  },
  {
    path: '/search',
    component: () => import(/* webpackChunkName:"Search" */'@/views/Search')
  },
  {
    path: '/search/:keywords',
    component: () => import(/* webpackChunkName:"SearchResult" */ '@/views/Search/SearchResult.vue')
  },
  {
    path: '/user_edit',
    component: () => import(/* webpackChunkName:"UserEdit" */ '@/views/User/UserEdit.vue')
  },
  {
    path: '/chat',
    component: () => import(/* webpackChunkName:"Chat" */ '@/views/Chat')
  },
  {
    path: '/layout',
    component: () => import(/* webpackChunkName:"Layout" */ '@/views/Layout'),
    children: [
      {
        path: 'home',
        component: () => import(/* webpackChunkName:"Home" */ '@/views/Home')
      },
      {
        path: 'user',
        component: () => import(/* webpackChunkName:"User" */ '@/views/User/UserEdit.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (getToken()?.length > 0 && to.path === '/login') {
    next(false)
  } else {
    // 其他放行
    next()
  }
})

export default router
